package at.tuwien.seminar.scrum1.view.wicket.column;

import at.tuwien.seminar.scrum1.model.Bill;
import at.tuwien.seminar.scrum1.model.Customer;
import at.tuwien.seminar.scrum1.model.Reservation;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.tree.table.IRenderable;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;


public class CustomerColumn<T> extends AbstractColumn<T> {


    public CustomerColumn(IModel<String> displayModel) {
        super(displayModel);

    }

    public void populateItem(Item<ICellPopulator<T>> item, String componentId, IModel<T> rowModel) {
        String customerString = "";
        List<Customer> customers = new ArrayList<Customer>();


        if (rowModel.getObject() instanceof Bill) {
            customers.addAll(((Bill) rowModel.getObject()).getCustomers());
        }

        if (rowModel.getObject() instanceof Reservation) {
            customers.addAll(((Reservation) rowModel.getObject()).getCustomers());
        }

        for (Customer cust : customers) {
            customerString += cust.getFirstName() + " " + cust.getLastName() + ";";
        }

        if (customerString.length() > 1) {
            customerString = customerString.substring(0, customerString.length()-1);
        }

        item.add(new Label(componentId, customerString));


    }


}
